package com.lxx.utils;

import java.util.HashMap;
import java.util.Map;

import com.lxx.common.ErrCode;

public class Result{
	
	public static final Result SUCCESS = new Result(ErrCode.SUCCESS);
	public static final Result ERROR = new Result(ErrCode.ERROR);
	public static final Result FAIL = new Result(ErrCode.FAIL);

	/**
	 * 状态码， 0--成功， 非0--失败
	 */
	private int code;
	
	private String msg;
	
	private Object data;
	
	private Map<String, Object> params = new HashMap<>();
	
	
	public static Result success(Object o) {
		Result result = new Result(0);
		result.setData(o);
		return result;
	}
	
	/**
	 * 返回成功
	 * @param o  数据体data
	 * @param params  返回额外的参数, k,v .. k,v的形式
	 * @return
	 */
	public static Result success(Object o, Object ... params) {
		Result result = new Result(0);
		result.setData(o);
		
		if (params.length % 2 == 0) {
			for (int i = 0; i < params.length; i+=2) {
				result.addParam(params[i].toString(), params[i+1]);
			}
		}
		
		return result;
	}
	
	public static Result fail(String msg) {
		Result result = new Result(ErrCode.FAIL.getCode());
		result.setMsg(msg);
		return result;
	}

	public Result() {
		super();
	}

	public Result(int code) {
		super();
		this.code = code;
	}

	public Result(int code, String msg) {
		super();
		this.code = code;
		this.msg = msg;
	}
	
	public Result(ErrCode errCode) {
		this.code = errCode.getCode();
		this.msg = errCode.getMsg();
	}

	
	public void addParam(String name, Object value) {
		params.put(name, value);
	}


	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public Map<String, Object> getParams() {
		return params;
	}

	public void setParams(Map<String, Object> params) {
		this.params = params;
	}
	
}
